import 'package:boss/ui/page/login/constant/login_type.dart';
import 'package:boss/ui/page/login/model/cache_user.dart';
import 'package:boss/ui/page/login/provider/login_provider.dart';
import 'package:boss/ui/widget/eye_icon.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/form/phone_code_form_field.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class LoginForm extends StatefulWidget {
  @override
  _LoginFormState createState() => _LoginFormState();
}

class _LoginFormState extends State<LoginForm> {
  TextEditingController _textEditingController;
  TextEditingController _pasEditingController;

  bool obscureText = true;

  @override
  void initState() {
    super.initState();
    _textEditingController = TextEditingController();
    _pasEditingController = TextEditingController();
    _loginCachePas();
  }

  _loginCachePas() async {
    CacheUser cacheUser = CacheUser.getCacheUser();
    if (cacheUser != null) {
      _textEditingController.text = cacheUser.loginUser.phone;
      _pasEditingController.text = cacheUser.loginUser.passWord;
    }
  }

  @override
  void dispose() {
    _textEditingController.dispose();
    _pasEditingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(
        top: ScreenUtil.instance.setHeight(45.5),
      ),
      padding: EdgeInsets.symmetric(
        horizontal: 65.w,
      ),
      child: Consumer<LoginProvider>(
        builder: (context, loginProvider, _) {
          return loginProvider.loginType == LoginType.userPassword ? _buildUserPasswordForm(loginProvider) : _buildPhoneCodeForm(loginProvider);
        },
      ),
    );
  }

  Widget _buildUserPasswordForm(LoginProvider loginProvider) {
    return Column(
      key: Key('login_user_password'),
      children: <Widget>[
        BaseTextFormField(
          hintText: '请输入你的手机号',
          textAlign: TextAlign.start,
          keyboardType: TextInputType.number,
          inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.numberReg)],
          controller: _textEditingController,
          onChanged: (value) {
            var loginProvider = Provider.of<LoginProvider>(
              context,
              listen: false,
            );
            loginProvider.setPhone(value);
          },
          isClearAll: true,
        ),
        BaseTextFormField(
          hintText: '请输入6-16位含数字字母密码',
          textAlign: TextAlign.start,
          controller: _pasEditingController,
          obscureText: obscureText,
          onChanged: (value) {
            var loginProvider = Provider.of<LoginProvider>(
              context,
              listen: false,
            );
            loginProvider.setPassword(value);
          },
          suffix: EyeIcon(
            value: obscureText,
            onTap: () {
              setState(() {
                obscureText = !obscureText;
              });
            },
          ),
        ),
      ],
    );
  }

  Widget _buildPhoneCodeForm(LoginProvider loginProvider) {
    return Column(
      key: Key('login_code'),
      children: <Widget>[
        BaseTextFormField(
          hintText: '请输入手机号',
          textAlign: TextAlign.start,
          keyboardType: TextInputType.number,
          inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.numberReg)],
          controller: _textEditingController,
          onChanged: (value) {
            var loginProvider = Provider.of<LoginProvider>(
              context,
              listen: false,
            );
            loginProvider.setPhone(value);
          },
          isClearAll: true,
        ),
        Consumer<LoginProvider>(
          builder: (context, loginProvider, child) {
            return PhoneCodeFormField(
              codeType: CodeType.loginVerifycode,
              onChanged: (value) {
                loginProvider.setCode(value);
              },
              phone: loginProvider.phone,
            );
          },
        ),
      ],
    );
  }
}
